using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FetCo.Data;
using ADESSA.Business;
using ADESSA.Entities;
using FetCo.Framework;
using FetCo.Web;

public partial class Admin_AllCompany : System.Web.UI.Page
{
    #region Region
    public int NoCountStart
    {
        get { return Convert.ToInt32(ViewState["NoCountStart"]); }
        set { ViewState["NoCountStart"] = value; }
    }
    protected string hidColSort
    {
        get
        {
            object o = ViewState["hidColSort"];
            if (o == null) return "[" + ADESSA.Entities.Company._Table + "].[" + CompanyColumns.CompanyID + "]";
            return o.ToString();
        }
        set
        {
            ViewState["hidColSort"] = value;
        }
    }
    public string ComName
    {
        get
        {
            if (HttpContext.Current.Request.QueryString["name"] == null)
                return "";
            else
                return HttpContext.Current.Request.QueryString["name"];
        }
    }
    public string ComFax
    {
        get
        {
            if (HttpContext.Current.Request.QueryString["fax"] == null)
                return "";
            else
                return HttpContext.Current.Request.QueryString["fax"];
        }
    }
    public string ComPhone
    {
        get
        {
            if (HttpContext.Current.Request.QueryString["phone"] == null)
                return "";
            else
                return HttpContext.Current.Request.QueryString["phone"];
        }
    }
    public long ComMem
    {
        get
        {
            if (HttpContext.Current.Request.QueryString["mem"] == null)
                return long.MinValue;
            else
                return long.Parse(HttpContext.Current.Request.QueryString["mem"]);
        }
    }
    public long ComCountry
    {
        get
        {
            if (HttpContext.Current.Request.QueryString["country"] == null)
                return long.MinValue;
            else
                return long.Parse(HttpContext.Current.Request.QueryString["country"]);
        }
    }
    public long ComSell
    {
        get
        {
            if (HttpContext.Current.Request.QueryString["sell"] == null)
                return long.MinValue;
            else
                return long.Parse(HttpContext.Current.Request.QueryString["sell"]);
        }
    }
    public long ComBuy
    {
        get
        {
            if (HttpContext.Current.Request.QueryString["buy"] == null)
                return long.MinValue;
            else
                return long.Parse(HttpContext.Current.Request.QueryString["buy"]);
        }
    }
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
            return;
        LoadCurent();
        LoadMember();
        LoadBuy();
        LoadCountry();
        LoadSell();
        LoadData();
    }
    #region LoadCurent
    private void LoadCurent()
    {
        HtmlGenericControl ulCompany = (HtmlGenericControl)Master.FindControl("ulCompany");
        ulCompany.Attributes.Add("class", "current");
        HtmlGenericControl liSubShowCom = (HtmlGenericControl)Master.FindControl("liSubShowCom");
        liSubShowCom.Attributes.Add("class", "sub_show");
        HtmlControl divSelectSubCom = (HtmlControl)Master.FindControl("divSelectSubCom");
        divSelectSubCom.Attributes.Add("class", "select_sub show");
    }
    #endregion
    #region LoadData
    private string username = string.Empty;
    private void LoadData()
    {
        Criteria cri = new Criteria();
        hyperlinkPager.PageSize = RTISettings.PageSize;
        NoCountStart = (hyperlinkPager.PageSize * (hyperlinkPager.CurrentIndex - 1)) + 1;
        int totalrecord = 0;
        if (!string.IsNullOrEmpty(this.ComName))
            cri.add("[" + Company._Table + "].[" + CompanyColumns.CompanyName + "]", this.ComName, Condition.LIKE, Condition.AND);
        if (!string.IsNullOrEmpty(this.ComPhone))
            cri.add("[" + Company._Table + "].[" + CompanyColumns.Phone + "]", this.ComPhone, Condition.LIKE, Condition.AND);
        if (!string.IsNullOrEmpty(this.ComFax))
            cri.add("[" + Company._Table + "].[" + CompanyColumns.Fax + "]", this.ComFax, Condition.LIKE, Condition.AND);
        if (this.ComSell != long.MinValue && this.ComSell != 2)
            cri.add("[" + Company._Table + "].[" + CompanyColumns.Sell + "]", this.ComSell, Condition.EQUAL, Condition.AND);
        if (this.ComBuy != long.MinValue && this.ComBuy != 2)
            cri.add("[" + Company._Table + "].[" + CompanyColumns.Buy + "]", this.ComBuy, Condition.EQUAL, Condition.AND);
        if (this.ComMem != long.MinValue && this.ComMem != 0)
            cri.add("[" + Company._Table + "].[" + CompanyColumns.MemberID + "]", this.ComMem, Condition.EQUAL, Condition.AND);
        if (this.ComCountry != long.MinValue && this.ComCountry != 0)
            cri.add("[" + Company._Table + "].[" + CompanyColumns.Country + "]", this.ComCountry, Condition.EQUAL, Condition.AND);
        CompanyCollection _CompanyCollection = CompanyService.GetCompanyListByParam(hidColSort, OrderDirection.ASC, hyperlinkPager.CurrentIndex, hyperlinkPager.PageSize, cri.Criter, out totalrecord);

        rptList.DataSource = _CompanyCollection;
        rptList.DataBind();
        hyperlinkPager.ItemCount = totalrecord;
        if ((float)totalrecord / (float)hyperlinkPager.PageSize <= 1)
            hyperlinkPager.Visible = false;

    }
    protected void rptList_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Company _Company = (Company)e.Item.DataItem;
            HiddenField hidID = (HiddenField)e.Item.FindControl("hidID");
            HtmlTableRow tr = (HtmlTableRow)e.Item.FindControl("tr");
            HtmlAnchor ahr = (HtmlAnchor)e.Item.FindControl("ahrMem");
            HtmlImage img = (HtmlImage)e.Item.FindControl("img");
            Literal ltrComName = (Literal)e.Item.FindControl("ltrComName");
            Literal ltrMemberName = (Literal)e.Item.FindControl("ltrMemberName");
            Literal ltrFax = (Literal)e.Item.FindControl("ltrFax");
            Literal ltrCountry = (Literal)e.Item.FindControl("ltrCountry");
            Literal ltrPhone = (Literal)e.Item.FindControl("ltrPhone");
            CheckBox chkSell = (CheckBox)e.Item.FindControl("chkSell");
            CheckBox chkBuy = (CheckBox)e.Item.FindControl("chkBuy");

            ahr.HRef = string.Format("AddMember.aspx?id={0}", _Company.MemberID);
            ltrComName.Text = _Company.CompanyName;
            ltrFax.Text = _Company.Fax;
            ltrPhone.Text = _Company.Phone;
            chkBuy.Checked = _Company.Buy;
            chkSell.Checked = _Company.Enable;
            Country _Country = CountryService.GetCountry(_Company.Country);
            if (_Country != null)
            {
                ltrCountry.Text = _Country.Name;
            }
            Member _Member = MemberService.GetMember(_Company.MemberID);
            if (_Member != null)
            {
                ltrMemberName.Text = _Member.FullName;
            }
            if (!string.IsNullOrEmpty(_Company.Logo))
                img.Src = RTISettings.NewsPictureFolder + "/" + _Company.Logo;

            hidID.Value = _Company.CompanyID.ToString();
            int STT = NoCountStart + e.Item.ItemIndex;
            if (STT % 2 != 0)
            {
                tr.Attributes["class"] = "alternate-row";
            }
            else
            {
                tr.Attributes["class"] = "none";
            }
        }

    }
    protected void pager_Command(object sender, CommandEventArgs e)
    {
        int currnetPageIndx = Convert.ToInt32(e.CommandArgument);
        hyperlinkPager.CurrentIndex = currnetPageIndx;
        LoadData();
    }
    protected void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string commandName = e.CommandName;
        object obj = new object();
        switch (commandName)
        {
            case "sortName":
                obj = "[" + Company._Table + "].[" + CompanyColumns.CompanyName + "]";
                break;
            case "SortOrder":
                obj = "[" + Company._Table + "].[" + CompanyColumns.MemberID + "]";
                break;
            default:
                break;
        }
        if (OrderDirection.ASC == OrderDirection.ASC)
            Session["Order"] = OrderDirection.DESC;
        else
            Session["Order"] = OrderDirection.ASC;
        this.hidColSort = obj.ToString();
        LoadData();
    }
    #endregion
    #region Delete Account
    protected void lndelete_Click(object sender, EventArgs e)
    {
        foreach (RepeaterItem item in this.rptList.Items)
        {
            CheckBox chkDelete = (CheckBox)item.FindControl("chkDelete");
            if (chkDelete.Checked == true)
            {
                HiddenField hidID = (HiddenField)item.FindControl("hidID");
                string strhidID = hidID.Value;
                long ID = long.Parse(strhidID);
                CompanyService.DeleteCompany(ID);
            }
        }
        LoadData();
    }
    #endregion
    #region Update
    protected void linkUpdate_Click(object sender, EventArgs e)
    {
        foreach (RepeaterItem item in this.rptList.Items)
        {
            HiddenField hidID = (HiddenField)item.FindControl("hidID");
            CheckBox chkSell = (CheckBox)item.FindControl("chkSell");
            CheckBox chkBuy = (CheckBox)item.FindControl("chkBuy");
            Company _Company = CompanyService.GetCompany(long.Parse(hidID.Value));


            if (_Company != null)
            {

                _Company.Enable = chkSell.Checked;
                _Company.Buy = chkBuy.Checked;
                CompanyService.UpdateCompany(_Company);
            }
        }
        LoadData();
    }
    #endregion

    #region Search
    protected void btnSearch_OnClick(object sender, EventArgs e)
    {
        Response.Redirect(string.Format("AllCompany.aspx?name={0}&fax={1}&phone={2}&mem={3}&country={4}&sell={5}&buy={6}", txtNameSearch.Text.Trim(), txtFaxSearch.Text.Trim(), txtPhoneSearch.Text.Trim(), ddlMemSearch.SelectedValue, ddlCountrySearch.SelectedValue, ddlSellSearch.SelectedValue, ddlBuySearch.SelectedValue));
    }
    protected void btnSearchAll_OnClick(object sender, EventArgs e)
    {
        Response.Redirect("AllCompany.aspx");
    }
    #endregion
  
    #region Load DDL Gender And Status

    protected void LoadBuy()
    {

        ddlBuySearch.Items.Add(new ListItem("--Select--", "2"));
        ddlBuySearch.Items.Add(new ListItem("True", "1"));
        ddlBuySearch.Items.Add(new ListItem("False", "0"));

    }
    protected void LoadSell()
    {

        ddlSellSearch.Items.Add(new ListItem("--Select--", "2"));
        ddlSellSearch.Items.Add(new ListItem("True", "1"));
        ddlSellSearch.Items.Add(new ListItem("False", "0"));

    }
    #endregion
    #region LoadCountry
    protected void LoadCountry()
    {

        ddlCountrySearch.Items.Clear();
        ddlCountrySearch.Items.Add(new ListItem("- - Select  Country- - -", "0"));
        CountryCollection _CountryCollection = CountryService.GetCountryList(CountryColumns.Order, OrderDirection.ASC);

        if (_CountryCollection.Count > 0)
        {
            foreach (Country _Country in _CountryCollection)
            {
                ddlCountrySearch.Items.Add(new ListItem(_Country.Name, _Country.CountryId.ToString()));
            }
        }
    }

    #endregion
    #region Load Member
    protected void LoadMember()
    {

        ddlMemSearch.Items.Clear();
        ddlMemSearch.Items.Add(new ListItem("- - Select  Member- - -", "0"));
        MemberCollection _MemberCollection = MemberService.GetMemberList(MemberColumns.MemberID, OrderDirection.ASC);

        if (_MemberCollection.Count > 0)
        {
            foreach (Member _Member in _MemberCollection)
            {
                ddlMemSearch.Items.Add(new ListItem(_Member.FullName, _Member.MemberID.ToString()));
            }
        }
    }
    #endregion
}
